package com.pxst.fo.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.fo.sys.entity.FoOrderMainEntity;
import com.pxst.fo.sys.mapper.SysFoOrderMainMapper;
import com.pxst.fo.sys.req.FoOrderMainReq;
import com.pxst.fo.sys.resp.FoOrderMainDownloadResp;
import com.pxst.fo.sys.resp.FoOrderMainResp;
import com.pxst.fo.sys.service.FoOrderMainService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.AltIdUtils;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

import static com.pxst.fo.sys.constants.FoConstants.*;

@Service
@Transactional
public class FoOrderMainServiceImpl extends JoinServiceImpl<SysFoOrderMainMapper, FoOrderMainEntity> implements FoOrderMainService {

    @Autowired
    private SysFoOrderMainMapper foOrderMainMapper;


    @Override
    public ApiResp<PageResp<FoOrderMainResp>> selectPage(FoOrderMainReq foOrderMainReq) {
        PageHelper.startPage(foOrderMainReq.getPage(), foOrderMainReq.getRp());
        List<FoOrderMainResp> foOrderMainResp = foOrderMainMapper.selectPage(foOrderMainReq);
        for (FoOrderMainResp orderMainResp : foOrderMainResp) {
            String w = AltIdUtils.getAltId("W-", orderMainResp.getCreatedAt(), orderMainResp.getFoOrderMainId());
            orderMainResp.setAltId(w);
        }
        Page<FoOrderMainResp> pageResp = (Page<FoOrderMainResp>) foOrderMainResp;
        return ApiResp.page(pageResp);
    }

    @Override
    public List<FoOrderMainDownloadResp> download(FoOrderMainReq foOrderMainReq) {
        List<FoOrderMainResp> foOrderMainResps = foOrderMainMapper.selectPage(foOrderMainReq);
        FoOrderMainDownloadResp downloadResp = new FoOrderMainDownloadResp();
        List<FoOrderMainDownloadResp> list = new ArrayList<>();
        for (FoOrderMainResp orderMainResp : foOrderMainResps) {
            String w = AltIdUtils.getAltId("W-", orderMainResp.getCreatedAt(), orderMainResp.getFoOrderMainId());
            downloadResp.setAltId(w);
            downloadResp.setMerchantCode(orderMainResp.getMerchantInfo());
            downloadResp.setBank(orderMainResp.getToBank());
            downloadResp.setBeneficiaryName(orderMainResp.getToMan());
            downloadResp.setAccountNumber(orderMainResp.getToCardNumber());
            downloadResp.setRequestAmount(orderMainResp.getRequestAmount());
            downloadResp.setPaidAmount(orderMainResp.getPaidAmount());
            downloadResp.setFee(orderMainResp.getRate().toString());
            downloadResp.setBankFee(orderMainResp.getBankFee());
            downloadResp.setPayBankFee(payBankFee(orderMainResp));
            downloadResp.setInfo(info(orderMainResp));
            downloadResp.setStatus(status(orderMainResp));
            downloadResp.setNtStatus(orderMainResp.getContent().toString());
            downloadResp.setCreatedAt(orderMainResp.getCreatedAt());
            downloadResp.setSuccess(orderMainResp.getCompletedAt());
            downloadResp.setUpdatedAt(orderMainResp.getUpdatedAt());
            list.add(downloadResp);
        }

        return list;
    }

    private String status(FoOrderMainResp foOrderMainResp){
        Map<Integer,String> map = new HashMap<>();
        map.put(STATUS_PENDING, "Pending/等待中");
        map.put(STATUS_PROCESSING, "Processing/处理中");
        map.put(STATUS_COMPLETED, "Success/成功");
        map.put(STATUS_MANUAL_PROCESS, "Manual process/手动流程");
        map.put(STATUS_FAILED, "Failed/失败");
        return map.get(foOrderMainResp.getStatus());
    }

    private String payBankFee(FoOrderMainResp foOrderMainResp){
        Map<Integer,String> payBankFee = new HashMap<>();
        payBankFee.put(PAY_BANK_FEE_ONE_WALLET, "one wallet/壹支付");
        payBankFee.put(PAY_BANK_FEE_MERCHANT, "merchant/商户");
        return payBankFee.get(foOrderMainResp.getPayBankFee());
    }

//    private String statusDesc(FoOrderMainResp foOrderMainResp){
//        Map<Integer,String> statusDesc = new HashMap<>();
//        statusDesc.put(STATUS_DESCRIPTION_BALANCE_NOT_ENOUGH, "Merchant's AVL Bal. is insufficient./商户可用余额不足");
//        statusDesc.put(STATUS_DESCRIPTION_NO_CARD, "No card available/没有可用的卡");
//        return statusDesc.get(foOrderMainResp.getStatusDescription());
//    }

    private String confirmAccname(FoOrderMainResp foOrderMainResp){
        Map<Integer,String> confirmAccname = new HashMap<>();
        confirmAccname.put(CONFIRM_ACCNAME_NO_AUDIT_REQUIRED, "No audit required/不需要审核");
        confirmAccname.put(CONFIRM_ACCNAME_PENDING_FINANCIAL_REVIEW, "Pending financial review/待财务审核");
        confirmAccname.put(CONFIRM_ACCNAME_SUBJECT_TO_MERCHANT_REVIEW, "Subject to merchant review/待商户审核");
        confirmAccname.put(CONFIRM_ACCNAME_TURN_DOWN, "turn down/拒绝");
        confirmAccname.put(CONFIRM_ACCNAME_PASS, "pass/通过");
        return confirmAccname.get(foOrderMainResp.getConfirmAccname());
    }

    private String info(FoOrderMainResp foOrderMainResp){
        String merchantInfo = foOrderMainResp.getMerchantInfo();
        String note = foOrderMainResp.getNote();
        String createdAt = foOrderMainResp.getCreatedAt().toString();
        String checkedMan = foOrderMainResp.getCheckedMan();
        String string = confirmAccname(foOrderMainResp);
        return ("MOID/商户订单号:"+merchantInfo+"/备注:"+note+createdAt+checkedMan+string);
    }

    public List<BigDecimal> count(FoOrderMainReq foOrderMainReq) {
        BigDecimal bigDecimal = new BigDecimal(0);
        BigDecimal bigDecimal1 = new BigDecimal(0);
        BigDecimal bigDecimal2 = new BigDecimal(0);
        BigDecimal bigDecimal3 = new BigDecimal(0);
        List<BigDecimal> list = new ArrayList<>();

        List<FoOrderMainResp> foOrderMainResp = foOrderMainMapper.selectPage(foOrderMainReq);
        for (FoOrderMainResp orderMainResp : foOrderMainResp) {
            bigDecimal.add(orderMainResp.getRequestAmount());
            bigDecimal1.add(orderMainResp.getPaidAmount());
            bigDecimal2.add(orderMainResp.getBankFee());
            bigDecimal3.add(orderMainResp.getRate());
        }
        list.add(bigDecimal);
        list.add(bigDecimal1);
        list.add(bigDecimal2);
        list.add(bigDecimal3);
        return list;
    }


    public List<BigDecimal> count1(FoOrderMainReq foOrderMainReq) {

        BigDecimal bigDecimal = new BigDecimal(0);
        BigDecimal bigDecimal1 = new BigDecimal(0);
        BigDecimal bigDecimal2 = new BigDecimal(0);
        BigDecimal bigDecimal3 = new BigDecimal(0);

        int a = 0;
        int b = 0;
        List<FoOrderMainResp> foOrderMainResp = foOrderMainMapper.selectPage(foOrderMainReq);
        for (FoOrderMainResp orderMainResp : foOrderMainResp) {
            a++;
            if (a > ((foOrderMainReq.getPage()-1)*foOrderMainReq.getRp())){
                bigDecimal.add(orderMainResp.getRequestAmount());
                bigDecimal1.add(orderMainResp.getPaidAmount());
                bigDecimal2.add(orderMainResp.getBankFee());
                bigDecimal3.add(orderMainResp.getRate());
                b++;
                if (b > foOrderMainReq.getRp() || a > foOrderMainResp.size()){
                    break;
                }
            }
        }
        List<BigDecimal> list = new ArrayList<>();
        list.add(bigDecimal);
        list.add(bigDecimal1);
        list.add(bigDecimal2);
        list.add(bigDecimal3);
        return list;
    }



}
